# Python 常用标准库
'''
模块            描述
os             操作系统管理
sys            解释器交互
platform       操作系统信息
glob           查找文件
shutil         文件管理
random         随机数
subprocess     执行Shell命令
pickle         对象数据持久化
json           JSON编码和解码
time           时间访问和转换
datetime       日期和时间
urllib         HTTP访问
'''
# 官方文档标准库列表：https://docs.python.org/zh-cn/3.8/library/index.html

'''
import os

print(os.name)
print(os.altsep)
print(os.path)
print(os.getcwd())
current = os.getcwd()
#module_dir = os.path.join(current_dir, "m")


import sys

print("hello world")
sys.exit(2)
print("hello2 world")
'''

'''
import platform
print(platform.platform())
print(platform.uname())
print(platform.system())
print(platform.version())
print(platform.mac_ver())
'''



#import pickle
#computer = {"主机":5000,"显示器":1000,"鼠标":60,"键盘":150}
#with open("data.pkl", "wb") as f:
#    pickle.dump(computer,f)

'''
import pickle
with open("data.pkl", "rb") as f:
    d = pickle.load(f)
print(d)
'''

'''
import json
#编码
computer = {"主机":5000,"显示器":1000,"鼠标":60,"键盘":150}
json_dbj = json.dumps(computer)
print(type(json_dbj))
print(computer)
print(json_dbj)

#解码
data = json.loads(json_dbj)
print(data)
'''

'''
import time
print(time.time())  #获取当前时间戳
print(time.strftime("%Y-%m-%d %H:%M:%S"))   #指定格式时间，例如2020-12-17 13:12:08
print(time.localtime())    #获取时间类型
#将时间戳转换指定格式
now = time.time()       #时间戳 -> struct -> strftime
struct_time = time.localtime(now)
print(time.strftime("%Y-%m-%d %H:%M:%S", struct_time))
#休眠2秒
d1 = time.time()
time.sleep(2)
d2 = time.time()
print(d2-d1)
'''

'''
from datetime import date, datetime
#print(datetime.now())
now = datetime.now()
#print(data.strftime(now, "%Y-%m-%d %H:%M:%S"))
print(date.today())

import time
date_array = datetime.fromtimestamp(time.time())
print(date_array)

yesterday = date.today() - timedelta(days=3)
print(yesterday)
tomorrow = date.today() + timedelta(days=1)
print(tomorrow)
'''


#urllib库用于访问URL。
import urllib
from urllib import request
res = request.urlopen("http://www.baidu.com")
print(res.getcode())   #获取响应得HTTP状态码
print(res.geturl())    #返回真实得URL。有可能URL3xx跳转，那么这个将获得跳转后的URL
print(res.headers)     #返回服务器header信息
#print(res.read())     #返回网页所有内容
print(res.readlines())

url = ["http://www.baidu.com","http://www.jd.com"]
for i in url:
    res = request.urlopen(i)
    if res.getcode() == 200 or res.getcode() == 301:
        print(f"{i}URL访问成功")
    else:
        print(f"{i}URL访问失败")


'''
# 示例1：自定义用户代理
from urllib import request
url = "http://www.ctnrs.com"
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108Safari/537.36"
header = {"User-Agent": user_agent}
req = request.Request(url, headers=header)
res = request.urlopen(req)
print(res.getcode())

#示例2： 向接口提交用户数据
from urllib import request, parse
url = "http://www.ctnrs.com/login"
post_data = {"username":"user1","password":"123456"}
post_data = parse.urlencode(post_data).encode("utf8") #将字典转为URL查询字符串格式，并转为bytes类型
req = request.Request(url, data=post_data, headers=header)
res = request.urlopen(req)
print(res.read())
'''